    package com.edu.bhu.admin.controller.admin;

import java.util.List;

import com.edu.bhu.common.config.OverallConfig;
import com.edu.bhu.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.edu.bhu.common.annotation.Log;
import com.edu.bhu.common.enums.BusinessType;
import com.edu.bhu.admin.domain.CosCosmetics;
import com.edu.bhu.admin.service.ICosCosmeticsService;
import com.edu.bhu.common.core.controller.BaseController;
import com.edu.bhu.common.core.domain.AjaxResult;
import com.edu.bhu.common.utils.poi.ExcelUtil;
import com.edu.bhu.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

    /**
 * 商品Controller
 * 
 * @author wangjinao
 * @date 2022-03-17
 */
@Controller
@RequestMapping("/admin/cosmetics")
public class CosCosmeticsController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(CosCosmeticsController.class);

    private String prefix = "admin/cosmetics";

    @Autowired
    private ICosCosmeticsService cosCosmeticsService;

    /**
     * 进入商品管理页
     */
    @RequiresPermissions("admin:cosmetics:view")
    @GetMapping()
    public String cosmetics()
    {
        return prefix + "/cosmetics";
    }

    /**
     * 进入商品列表页
     */
    @RequiresPermissions("admin:cosmetics:view")
    @GetMapping("/cosmeticsCard")
    public String houseCard()
    {
        return prefix + "/cosmeticsCard";
    }

    /**
     * 查询商品列表
     */
    @RequiresPermissions("admin:cosmetics:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CosCosmetics cosCosmetics)
    {
        startPage();
        List<CosCosmetics> list = cosCosmeticsService.selectCosCosmeticsList(cosCosmetics);
        return getDataTable(list);
    }

    /**
     * 查询商品列表(卡片)
     */
    @RequiresPermissions("admin:cosmetics:list")
    @PostMapping("/listCard")
    @ResponseBody
    public TableDataInfo listCard(CosCosmetics cosCosmetics)
    {
        cosCosmetics.setStatus(1L);
        startPage();
        List<CosCosmetics> list = cosCosmeticsService.selectCosCosmeticsList(cosCosmetics);
        return getDataTable(list);
    }

    /**
     * 进入某个商品的详细信息页
     */
    @GetMapping("/cosmeticsInfo/{cosId}")
    public String cosmeticsInfo(@PathVariable("cosId") Long cosId, ModelMap mmap)
    {
        //拿到商品信息
        CosCosmetics cosCosmetics = cosCosmeticsService.selectCosCosmeticsByCosId(cosId);
        mmap.put("cosmetics", cosCosmetics);
        return prefix + "/cosmeticsInfo";
    }

    /**
     * 填写数量的弹框（加入购物车）
     */
    @RequiresPermissions("admin:cosmetics:view")
    @GetMapping("/goAddCart/{cosId}")
    public String goAddCart(@PathVariable("cosId") String cosId, ModelMap mmap)
    {
        mmap.put("cosId",cosId);
        //拿到商品信息
        CosCosmetics cosCosmetics = cosCosmeticsService.selectCosCosmeticsByCosId(Long.valueOf(cosId));
        mmap.put("cosmetics",cosCosmetics);
        return prefix + "/addCart";
    }

    /**
     * 填写数量的弹框（直接生成订单）
     */
    @RequiresPermissions("admin:cosmetics:view")
    @GetMapping("/goAddOrder/{cosId}")
    public String goAddOrder(@PathVariable("cosId") String cosId, ModelMap mmap)
    {
        mmap.put("cosId",cosId);
        //拿到商品信息
        CosCosmetics cosCosmetics = cosCosmeticsService.selectCosCosmeticsByCosId(Long.valueOf(cosId));
        mmap.put("cosmetics",cosCosmetics);
        return prefix + "/addOrder";
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("admin:cosmetics:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CosCosmetics cosCosmetics)
    {
        List<CosCosmetics> list = cosCosmeticsService.selectCosCosmeticsList(cosCosmetics);
        ExcelUtil<CosCosmetics> util = new ExcelUtil<CosCosmetics>(CosCosmetics.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品
     */
    @RequiresPermissions("admin:cosmetics:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CosCosmetics cosCosmetics,@RequestParam("picture") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(OverallConfig.getAvatarPath(), file);
                cosCosmetics.setImage(avatar);
            }
            if (cosCosmeticsService.insertCosCosmetics(cosCosmetics) > 0)
            {
                return success();
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("更新商品失败！", e);
            return error(e.getMessage());
        }
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("admin:cosmetics:edit")
    @GetMapping("/edit/{cosId}")
    public String edit(@PathVariable("cosId") Long cosId, ModelMap mmap)
    {
        CosCosmetics cosCosmetics = cosCosmeticsService.selectCosCosmeticsByCosId(cosId);
        mmap.put("cosCosmetics", cosCosmetics);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("admin:cosmetics:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CosCosmetics cosCosmetics,@RequestParam("picture") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(OverallConfig.getAvatarPath(), file);
                cosCosmetics.setImage(avatar);
            }
            if (cosCosmeticsService.updateCosCosmetics(cosCosmetics) > 0)
            {
                return success();
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("更新商品失败！", e);
            return error(e.getMessage());
        }
    }

    /**
     * 商品状态修改
     */
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RequiresPermissions("admin:cosmetics:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(CosCosmetics cosCosmetics)
    {
        return toAjax(cosCosmeticsService.changeStatus(cosCosmetics));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("admin:cosmetics:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cosCosmeticsService.deleteCosCosmeticsByCosIds(ids));
    }
}
